package com.flynn.rock.system.convert;

import cn.hutool.core.date.DatePattern;
import com.flynn.rock.system.entity.SysMessageEntity;
import com.flynn.rock.system.model.dto.SaveMessageDto;
import com.flynn.rock.system.model.vo.MessageVo;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.ReportingPolicy;
import org.mapstruct.factory.Mappers;

@Mapper(unmappedTargetPolicy = ReportingPolicy.IGNORE)
public abstract class AbstractMessageConvert {

    public static final AbstractMessageConvert INSTANCE = Mappers.getMapper(AbstractMessageConvert.class);

    public abstract SysMessageEntity toMessageEntity(SaveMessageDto dto);

    @Mapping(target = "createTime", dateFormat = DatePattern.NORM_DATETIME_PATTERN)
    @Mapping(target = "readMessageTime", dateFormat = DatePattern.NORM_DATETIME_PATTERN)
    @Mapping(target = "readMessage", expression = "java(java.util.Objects.nonNull(entity.getReadMessageTime()))")
    public abstract MessageVo toMessageVo(SysMessageEntity entity);
}
